<template>
	<view>
	<custom-navbar title="预览"></custom-navbar>
	<view class="fileDetail">
		<view class="readList" v-if="readList.length > 0">
			<view class="read_item" v-for="(item,index) in readList" :key="index">
				{{item.viewUser}} 已于 {{item.viewTime}}查看了文件
			</view>
		</view>
		<template v-if="['png','jpg'].includes(fileTypeVal)">
			<view class="imageWrap">
				<image :src="fileUrlVal" mode="widthFix"></image>
			</view>
		</template>
			<ofd-preview :ofdLink="fileUrlVal" v-if="fileTypeVal === 'ofd'"></ofd-preview>
		 <pdf-preview :pdfLink="fileUrlVal" v-if="fileTypeVal === 'pdf'"></pdf-preview>
		 <docx-preview :docxLink="fileUrlVal" v-if="fileTypeVal === 'docx'"></docx-preview>
		 	<image src="../../static/img/fileDown.png" mode="" class="downLoadFile" @tap="downLoadFile" v-if="isShowDownload && roleKey !== 'BureauOfJustice'"></image>
	</view>
	</view>
</template>

<script setup>
import {onLoad,onReady,onUnload} from '@dcloudio/uni-app'
import {readComplete,viewDocument,seeDocumentRecord,fileRead,fileScan,addViewRecord} from '@/api/document.js'
import {getSaveRole} from '@/utils/auth.js'
import {ref} from 'vue'
const fileTypeVal = ref("")
const fileUrlVal = ref("")
const deliverIdVal = ref("")
const isShowRepic = ref("0")
const deliveryFileIdVal = ref("")
const isShowDownload = ref(false)
const roleKey = ref("")
onLoad((data)=>{
	const {fileOriginalName,fileType,fileUrl,deliverId,isToRepict,deliveryFileId,isDownLoad} = data
	deliveryFileIdVal.value = deliveryFileId
	isShowRepic.value  = isToRepict || false
	isShowDownload.value = isDownLoad === "true" ? true :false
	uni.setNavigationBarTitle({
	    title: fileOriginalName
	  });
	  fileTypeVal.value = fileType
	  fileUrlVal.value = fileUrl
	  deliverIdVal.value = deliverId
	  init()
})
const init = ()=>{
	const role = getSaveRole()
	roleKey.value = role
	switch(role){
		case "BureauOfJustice":
		diffLawRead()
		sfFileScan()
		break;
		case "Procuratorate":
		seeRecordList()
		break;
		case "AdministrativeAgencies":
		fileReadAPi()
		break;
	}
	
}
const sfFileScan = async()=>{
const {code,data:res} = await fileScan({
	deliveryId:deliverIdVal.value,
	deliveryFileId:deliveryFileIdVal.value
})
if(code === 200){
	if(res){
		console.log("阅读成功！");
	}
}
}
// // 添加司法局查看记录
const diffLawRead = async()=>{
	const {code,data:res} = await addViewRecord({
		id:deliverIdVal.value,
		deliveryFileId:deliveryFileIdVal.value
	})
	if(code === 200){
		if(res){
			console.log("添加阅读记录成功！");
		}
	}
}

// 获取检察建议查看记录
const readList = ref([])
const seeRecordList = async()=>{
const {code,data:res} = await seeDocumentRecord({
		id:deliverIdVal.value,
		deliveryFileId:deliveryFileIdVal.value
	})
	if(code === 200){
		readList.value = res
			console.log("阅读记录！",res);
	}
} 


// 获取文件阅读完成接口
const fileReadAPi = async()=>{
	try{
		const {code,data:res} = await fileRead({
			deliveryId:deliverIdVal.value,
			deliveryFileId:deliveryFileIdVal.value
		})
		if(code === 200){
			if(res){
				console.log("阅读成功");
			}
		}
	}catch(e){
		//TODO handle the exception
	}
}
const downLoadFile = ()=>{
	// #ifdef H5
	// H5环境下，创建下载链接
	uni.downloadFile({
		url:fileUrlVal.value,
		success(res) {
			console.log('下载文件成功！', res)
			if (res.statusCode === 200) {
				const tempFilePath = res.tempFilePath
				// #ifdef H5
				// H5环境下，创建下载链接
				const a = document.createElement('a')
				a.href = tempFilePath
				a.download = `文件.${fileTypeVal.value}` // 替换为实际的文件名
				document.body.appendChild(a)
				a.click()
				document.body.removeChild(a)
				uni.hideLoading()
				uni.showToast({ title: '下载完成', icon: 'success' })
				// #endif
			}
		},
	})
	// #endif
}
</script>

<style lang="scss" scoped>
.fileDetail{
	position: relative;
	height:calc(100vh) ;
	width: 100vw;
	display: flex;
	flex-direction: column;
	background: #ededf0;
	image{
		width: 100%;
	}
}
.readBtn{
	width: 100%;
	padding: 10rpx 35rpx 30rpx;
}
.readList{
	background: #0D53FE;
	border-radius: 16rpx;
	padding: 20rpx;
	margin: 32rpx;
	font-weight: 400;
	font-size: 24rpx;
	color: #FFFFFF;
	line-height: 33rpx;
	.read_item{
		margin-bottom: 16rpx;
		&:last-child{
			margin-bottom: 0;
		}
	}
}
.imageWrap{
	padding: 0 35rpx;
}
.downLoadFile{
	position: absolute;
	left: 3%;
	bottom: 3%;
	width: 80rpx !important;
	height: 80rpx !important;
	z-index:  9999;
}
</style>
